package com.quartz.springbootquartz.web;

import com.quartz.springbootquartz.simple.MyJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author longdeng
 * @Date 2020/7/27 0027 22:57
 */
@Component
public class QuartzService {

    @Autowired
    private SchedulerFactory schedulerFactory;

    /**
     * 启动quartz工具类
     *
     * @return
     */
    public Map<String, Object> start() throws SchedulerException {

        JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
                .withIdentity("myJob", "myGroup")
                .usingJobData("name", "zhxl")
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                // 触发器标识
                .withIdentity("trigger1", "trigger1")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(2).repeatForever())
                .startNow()
                .build();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();

        Map<String, Object> map = new HashMap<>();

        map.put("code", "200");
        map.put("msg", "quartz定时调度任务启动成功");
        return map;

    }

    /**
     * 停止定时调度任务
     *
     * @param name  job名称
     * @param group job的工作组
     * @return
     */
    public Map<String, Object> parseJob(String name, String group) throws SchedulerException {
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.pauseJob(new JobKey(name, group));
        Map<String, Object> success = ResultMapUtil.success();
        success.put("msg", "关闭定时调度任务，名称[" + name + "],所属组[" + group + "]");
        return success;
    }

    /**
     * 启动定时调度任务
     *
     * @param name  job名称
     * @param group 所属工作组
     * @return
     */
    public Map<String, Object> startJob(String name, String group) throws SchedulerException {
        Map<String, Object> success = ResultMapUtil.success();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.resumeJob(new JobKey(name, group));
        success.put("msg", "当前定时调度任务所属组[" + group + "],名称name[" + name + "]启动成功");
        return success;
    }

    /**
     * 删除quartz的定时调度任务
     *
     * @param name  job名称
     * @param group 所属工作组
     * @return
     */
    public Map<String, Object> deleteJob(String name, String group) throws SchedulerException {
        Map<String, Object> success = ResultMapUtil.success();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.deleteJob(new JobKey(name, group));
        success.put("msg", "quartz定时调度任务所属组[" + group + "]中的任务名称[" + name + "]删除成功");
        return success;
    }

    /**
     * 添加定时调度任务
     *
     * @param name  job名称
     * @param group 所属工作组
     * @return
     */
    public Map<String, Object> addJob(String name, String group) throws SchedulerException {
        Map<String, Object> success = ResultMapUtil.success();
        Scheduler scheduler = schedulerFactory.getScheduler();

        JobDetail jobDetail1 = scheduler.getJobDetail(new JobKey(name, group));
        if (Objects.nonNull(jobDetail1)) {
            success.put("msg", "当前所属分组[" + group + "]的job名称[" + name + "]已经存在于quartz定时调度任务中," +
                    "建议启动即可");
            return success;
        }
        JobDetail jobDetail = JobBuilder.newJob(MyJob2.class)
                .withIdentity(name, group)
                .usingJobData("name", "zhxl")
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                // 触发器标识
                .withIdentity("trigger2", "trigger1")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(5).repeatForever())
                .startNow()
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        return success;
    }
}
